//package com.lengxf.gateway.bak;
//
//import cn.hutool.core.bean.BeanUtil;
//import cn.hutool.json.JSONUtil;
//import com.lengxf.gateway.model.GateWayConstant;
//import com.lengxf.gateway.model.ProxyModel;
//import lombok.Data;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.cloud.gateway.filter.GatewayFilter;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
//import org.springframework.core.io.buffer.DataBufferUtils;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpMethod;
//import org.springframework.http.MediaType;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.nio.charset.StandardCharsets;
//import java.util.Arrays;
//import java.util.Collections;
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//import java.util.stream.Collectors;
//
///**
// * 构建代理请求对象
// *
// * @Author Lengxf
// */
//@Slf4j
//@Component
//public class AcrossRequestProxyCacheGatewayFilterFactory extends AbstractGatewayFilterFactory<AcrossRequestProxyCacheGatewayFilterFactory.Config> {
//
//
//    public AcrossRequestProxyCacheGatewayFilterFactory() {
//        super(AcrossRequestProxyCacheGatewayFilterFactory.Config.class);
//    }
//
//
//    @Override
//    public GatewayFilter apply(Config config) {
//        return (exchange, chain) -> {
//            ServerHttpRequest request = exchange.getRequest();
//            HttpHeaders headers = request.getHeaders();
//            if (!(Boolean.getBoolean(headers.getFirst(config.getWrapperHeader())) && config.getNeedWrapper())) {
//                String method = request.getMethodValue();
//                if (HttpMethod.POST.matches(method)) {
//                    return doPostCache(exchange, chain);
//                } else if (HttpMethod.GET.matches(method)) {
//                    doGetCache(exchange, headers);
//                }
//            }
//            return chain.filter(exchange);
//        };
//    }
//
//    private void doCache(ServerWebExchange exchange, ProxyModel proxyModel) {
//        exchange.getAttributes().put(GateWayConstant.ACROSS_CACHE, proxyModel);
//    }
//
//    private void doGetCache(ServerWebExchange exchange, HttpHeaders headers) {
//        ServerHttpRequest request = exchange.getRequest();
//        Map<String, String> stringStringMap = request.getQueryParams().toSingleValueMap();
//        Map<String, Object> map = new ConcurrentHashMap<>();
//        BeanUtil.copyProperties(stringStringMap, map);
//        ProxyModel proxyModel = new ProxyModel().setPath(request.getURI().getPath())
//                .setMethod(HttpMethod.GET).setHeaders(headers).setParams(map);
//        doCache(exchange, proxyModel);
//    }
//
//    private Mono<Void> doPostCache(ServerWebExchange exchange, GatewayFilterChain chain) {
//        return DataBufferUtils.join(exchange.getRequest().getBody())
//                .flatMap(dataBuffer -> {
//                    byte[] bytes = new byte[dataBuffer.readableByteCount()];
//                    dataBuffer.read(bytes);
//                    String bodyString = new String(bytes, StandardCharsets.UTF_8);
//                    ServerHttpRequest request = exchange.getRequest();
//                    HttpHeaders headers = request.getHeaders();
//                    ProxyModel proxyModel = new ProxyModel().setPath(request.getURI().getPath())
//                            .setMethod(HttpMethod.POST).setHeaders(headers);
//                    if (StringUtils.isNotBlank(bodyString)) {
//                        List<String> contentTypeList = headers.getOrDefault(HttpHeaders.CONTENT_TYPE.toLowerCase(), Collections.emptyList());
//                        if (contentTypeList.contains(MediaType.APPLICATION_FORM_URLENCODED_VALUE)) {
//                            Map<String, Object> params = Arrays.stream(bodyString.split("&"))
//                                    .map(e -> e.split("=")).collect(Collectors.toMap(e -> e[0], e -> e[1]));
//                            proxyModel.setParams(params);
//                        } else if (contentTypeList.contains(MediaType.APPLICATION_JSON_VALUE)) {
//                            proxyModel.setParams(JSONUtil.toBean(bodyString, Map.class));
//                        }
//                    }
//                    doCache(exchange, proxyModel);
//                    DataBufferUtils.release(dataBuffer);
//                    return chain.filter(exchange);
//                });
//    }
//
//
//    @Override
//    public List<String> shortcutFieldOrder() {
//        return List.of("wrapperHeader", "needWrapper");
//    }
//
//    @Data
//    public static class Config {
//
//        private String wrapperHeader;
//
//        private Boolean needWrapper;
//
//    }
//
//
//}
